<template>
  <div v-show="option === 'login'" class="container">
    <el-form ref="formRef" :model="form" :rules="rules" class="form-bar" label-width="120px">
      <el-form-item prop="username">
        <el-input
            v-model="form.username"
            placeholder="请输入用户名"
            prefix-icon="Avatar"
            size="large"
        />
      </el-form-item>
      <el-form-item prop="password">
        <el-input
            v-model="form.password"
            placeholder="请输入密码"
            prefix-icon="Lock"
            show-password
            size="large"
            type="password"
            @keyup.enter="handleLogin()"
        />
      </el-form-item>
      <el-form-item>
        <el-row justify="space-between" style="width: 100%">
          <el-col :span="6">
            <el-checkbox v-model="form.rememberMe">
              记住我
            </el-checkbox>
          </el-col>
          <el-col :offset="6" :span="12">
            <el-link
                style="float: right"
                type="primary"
                @click="option = 'reset'"
            >忘记密码
            </el-link>
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item>
        <el-button
            :loading="loading"
            class="full-bar"
            size="large"
            type="primary"
            @click="handleLogin()"
        >登录
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script name="LoginInfo" setup>
import {getCurrentInstance, reactive, ref, toRefs} from "vue";
import {getTenantIdByName, getUserInfo, login} from "@/api/login";
import useUserInfoStore from "@/stores/userInfo";

const {proxy} = getCurrentInstance();

const option = defineModel()
const loading = ref(false);
const formRef = ref(null)

const data = reactive({
  form: {
    username: "admin",
    password: 'admin123',
    rememberMe: true,
    tenantName: '芋道源码',
  },
  rules: {
    username: [
      {required: true, message: "用户名不能为空", trigger: ["blur", "change"]}
    ],
    password: [
      {required: true, message: "密码不能为空", trigger: ["blur", "change"]}
    ],
  }
});

const {form, rules} = toRefs(data);

function handleLogin() {
  loading.value = true
  getTenantIdByName(form.value.tenantName)
  formRef.value.validate((valid) => {
        if (valid) {
          login(form.value).then(res => {
            if (res.data) {
              proxy.$modal.loading('正在加载系统中...')
              useUserInfoStore().setAccessToken(res.data.accessToken)
              useUserInfoStore().setRefreshToken(res.data.refreshToken)
              getUserInfo().then(res => {
                if (res.data) {
                  useUserInfoStore().setUser(res.data)
                  proxy.$router.push('/')
                }
              })
            }
          }).finally(() => {
            loading.value = false
            proxy.$modal.closeLoading()
          })
        }
      }
  )
}
</script>

<style lang="scss" scoped>
.container {
  height: calc(100% - 60px);
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;

  .form-bar {
    width: 500px;
  }
}
</style>